package cn.yunsenlin.formula.cloud.auth;

import cn.yunsenlin.formula.cloud.bean.auth.AuthResult;
import cn.yunsenlin.formula.cloud.consts.AuthConst;
import cn.yunsenlin.formula.cloud.interfaces.AuthService;
import cn.yunsenlin.formula.cloud.mould.ControllerMould;
import cn.yunsenlin.formula.cloud.mysql.bean.generator.TbUser;
import cn.yunsenlin.formula.cloud.responsibility.ContinueAbleRRH;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/auth")
@CrossOrigin
public class AuthController {

    private final AuthService authService;

    public AuthController(AuthService authService) {
        this.authService = authService;
    }

    /**
     * 获取鉴权结果
     *
     * @param authInfo 鉴权信息
     * @param response response
     * @param session  session
     * @return 鉴权结果
     */
    @RequestMapping(path = "/", method = RequestMethod.POST)
    public AuthResult getAuthResult(@RequestBody TbUser authInfo, HttpServletResponse response, HttpSession session) {
        return new ControllerMould(new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                AuthResult authResult = authService.checkAuthInfo(authInfo);
                session.setAttribute(AuthConst.AUTH_RESULT_SESSION_NAME, authResult);
                return authResult;
            }
        }) {
        }.startUpObject(response);
    }

    /**
     * 检查用户名是否存在
     *
     * @param username 用户名
     * @param response response
     * @return 是否存在
     */
    @RequestMapping(path = "/checkRepeatUsername/{username}", method = RequestMethod.POST)
    public boolean checkRepeatUsername(@PathVariable("username") String username, HttpServletResponse response) {
        return new ControllerMould(new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                return authService.userNameIsRepeat(username);
            }
        }) {
        }.startUpObject(response);
    }

    /**
     * 检查token
     *
     * @param token    token
     * @param response response
     * @param session  session
     * @return 鉴权结果（刷新后的）
     */
    @RequestMapping(path = "/checkToken", method = RequestMethod.POST)
    public AuthResult checkToken(@RequestBody String token, HttpServletResponse response, HttpSession session) {
        return new ControllerMould(new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                AuthResult authResult = authService.checkToken(token);
                session.setAttribute(AuthConst.AUTH_RESULT_SESSION_NAME, authResult);
                return authResult;
            }
        }) {
        }.startUpObject(response);
    }

    /**
     * 检查session
     *
     * @param response response
     * @param session  session
     * @return 鉴权结果（当前存储的）
     */
    @RequestMapping(path = "/checkSession", method = RequestMethod.POST)
    public AuthResult checkSession(HttpServletResponse response, HttpSession session) {
        return new ControllerMould(new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                if (authService.sessionHasValidAuth(session)) {
                    return session.getAttribute(AuthConst.AUTH_RESULT_SESSION_NAME);
                }
                return null;
            }
        }) {
        }.startUpObject(response);
    }

    /**
     * 退出系统
     *
     * @param response response
     * @param session  session
     */
    @RequestMapping(path = "/exit", method = RequestMethod.POST)
    public void exit(HttpServletResponse response, HttpSession session) {
        new ControllerMould(new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                session.removeAttribute(AuthConst.AUTH_RESULT_SESSION_NAME);
                return null;
            }
        }) {
        }.startUpObject(response);
    }

    /**
     * 注册用户
     *
     * @param tbUser   用户信息
     * @param response response
     * @return 注册的用户
     */
    @RequestMapping(path = "/createUser", method = RequestMethod.POST)
    public TbUser createUser(@RequestBody TbUser tbUser, HttpServletResponse response) {
        return new ControllerMould(new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                if (authService.userNameIsRepeat(tbUser.getUsername())) {
                    response.setStatus(405);
                }
                return authService.createUser(tbUser);
            }
        }) {
        }.startUpObject(response);
    }
}
